package com.inspur.dgov.vlbod.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inspur.dgov.vlbod.constant.UpAndCollectConstant;
import com.inspur.dgov.vlbod.entity.DemandCollect;
import com.inspur.dgov.vlbod.mapper.DemandCollectMapper;
import com.inspur.dgov.vlbod.service.DemandCollectService;
import com.inspur.dgov.vlbod.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * DemandCollectServiceImpl
 * 点播收藏
 * package com.inspur.dgov.vlbod.service.impl
 *
 * @author xie_yh
 * @date 2023/5/28 10:32
 */
@Slf4j
@Service
public class DemandCollectServiceImpl extends ServiceImpl<DemandCollectMapper, DemandCollect> implements DemandCollectService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${dgov.collect.count}")
    private Integer collectCount;

    @Override
    public boolean saveCollect(String broadcastId, String userId) {
        //1.查看是否收藏过
        String redisDemandCollectId = UpAndCollectConstant.DEMAND_COLLECT_ZSET + broadcastId;
        if (redisTemplate.opsForZSet().score(redisDemandCollectId, userId) != null) {
            return false;
        }

        //查看是否收藏达到上限
        long userCollectCount =
                this.lambdaQuery().eq(DemandCollect::getUserId, userId).count();
        log.debug("current user collect count:{}", userCollectCount);
        if (userCollectCount >= collectCount) {
            return false;
        }
        //保存收藏信息到数据库
        LocalDateTime currentDateTime = LocalDateTime.now();
        DemandCollect collect = new DemandCollect();
        collect.setDemandId(broadcastId);
        collect.setUserId(userId);
        collect.setCreateTime(currentDateTime);
        save(collect);
        //保存收藏信息到redis
        double timeStampFromLocalDateTime = TimeUtils.getTimeStampFromLocalDateTime(currentDateTime);
        redisTemplate.opsForZSet().add(redisDemandCollectId, userId, timeStampFromLocalDateTime);
        return true;
    }

    /**
     * 删除收藏
     *
     * @param broadcastId
     * @param userId
     */
    @Override
    public void deleteCollect(String broadcastId, String userId) {
        //数据库删除
        String redisDemandCollectId = UpAndCollectConstant.DEMAND_COLLECT_ZSET + broadcastId;
        lambdaUpdate().eq(DemandCollect::getDemandId, broadcastId)
                .eq(DemandCollect::getUserId, userId).remove();
        //redis删除
        redisTemplate.opsForZSet().remove(redisDemandCollectId, userId);
    }

    /**
     * 获取点播收藏总数
     *
     * @param broadcastId
     * @return
     */
    public Long getCollectCount(String broadcastId) {
        String redisDemandCollectId = UpAndCollectConstant.DEMAND_COLLECT_ZSET + broadcastId;
        Long count = redisTemplate.opsForZSet().size(redisDemandCollectId);
        return count != null ? count : 0;
    }

    /**
     * 判断是否已收藏
     * @param demandId 点播id
     * @param userId 用户id
     * @return
     */
    @Override
    public boolean isCollect(String demandId, String userId) {
        String redisDemandId = UpAndCollectConstant.DEMAND_COLLECT_ZSET + demandId;
        return redisTemplate.opsForZSet().score(redisDemandId, userId) != null;
    }
}
